import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '@/views/login.vue'
// import LoginAdmin from '@/views/login-old.vue'
// import UserPage from '@/views/userPage.vue'
// import Home from '../views/Home.vue'
import gis from './module/gis'
import report from './module/report'

import api from '@/apis/index.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: '登录',
    component: () => import('@/views/login.vue')
  },
  {
    path: '/login-admin',
    name: '管理员登录',
    component: () => import('@/views/login-old.vue')
  },
  {
    path: '/',
    redirect: '/home',
    component: () => import('../views/Home.vue'),
    children: [
      report,
      gis,
      {
        path: 'gisPage/intelligentOptimization',
        name: '智能优化',
        meta: {
          title: '智能优化',
          routeType: 2
        },
        component: () =>
          import(
            /* webpackChunkName:'gis'*/
            '@/views/gisPage/intelligentOptimization'
          )
      },
      // {
      //   path: 'gisPage/thematic-research',
      //   name: '专题研究',
      //   meta: {
      //     title: '专题研究',
      //     routeType: 2,
      //     keepAlive: false
      //   },
      //   component: () => import('@/views/gisPage/gisMap/content/index.vue')
      // },
      {
        path: 'gisPage/thematic-card',
        name: '专题研究',
        meta: {
          title: '专题研究',
          routeType: 2,
          keepAlive: false
        },
        component: () => import('@/views/gisPage/thematic-research/index.vue')
      },
      {
        path: 'gisPage/thematic-analyse',
        name: '新增分析',
        meta: {
          title: '新增分析',
          routeType: 2,
          keepAlive: false
        },
        component: () =>
          import('@/views/gisPage/thematic-research/page/analyse')
      },
      {
        path: 'gisPage/immersionAndOverflow',
        name: '漫入漫出',
        meta: {
          title: '漫入漫出',
          routeType: 2
        },
        component: () =>
          import(
            /* webpackChunkName:'gis'*/
            '@/views/gisPage/immersionAndOverflow'
          )
      }
    ]
  },
  {
    path: '/userPage',
    name: '',
    component: () => import('@/views/userPage.vue')
  },
  {
    path: '/hello',
    name: '',
    component: () => import('@/views/hello')
  }
]

const router = new VueRouter({
  mode: 'history',
  routes,
  base: '/'
})

router.beforeEach((to, from, next) => {
  router.formPath = from
  if (
    to.path === '/login' ||
    to.path === '/userPage' ||
    to.path == '/login-admin'
  ) {
    next()
  } else {
    let Authorization = localStorage.getItem('Authorization')
    // 设置过期时间  天
    let expiresDay = 1
    let time = expiresDay * 24 * 60 * 60 * 1000
    let startDate = sessionStorage.getItem('startDate')
    let ifExpires = new Date().getTime() - time > startDate

    if (Authorization === null || Authorization === '' || ifExpires) {
      next('/login')
    } else {
      let userInfoVo = JSON.parse(localStorage.getItem('userInfoVo'))
      if (!userInfoVo.authorities) {
        next('/login')
      }
      let authorities = userInfoVo.authorities.split(';')
      //路由权限鉴定
      if (authorities.includes(to.path)) {
        console.log(to.path, to.name)
        if (to.name) {
          let str = '查看' + to.name + '页面'
          api.updateOperatLog({ rout: str })
        }
        next()
      } else {
        next(authorities[0])
      }
      // next()
    }
  }
})
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

// router.onError(error => {
//   const pattern = /Loading chunk (\d)+ failed/g
//   const isChunkLoadFailed = error.message.match(pattern)
//   const targetPath = router.history.pending.fullPath
//   if (isChunkLoadFailed) {
//     router.replace(targetPath)
//   }
// })

export default router
